package com.woniu.controller.healthSystem;

import com.github.pagehelper.PageInfo;
import com.woniu.model.gh.common.Result;
import com.woniu.model.gh.common.enums.impl.BusinessCode;
import com.woniu.model.gh.common.exception.Asserts;
import com.woniu.model.gh.utils.TokenUtil;
import com.woniu.model.healthSystem.dto.EmployeeDto;
import com.woniu.model.healthSystem.dto.ManagerDto;
import com.woniu.model.healthSystem.entity.Manager;
import com.woniu.model.healthSystem.vo.TokenVo;
import com.woniu.service.healthSystem.IManagerServiceGb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 账号表 前端控制器
 * </p>
 *
 * @author gb
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/manager")
public class ManagerControllerGb {

    @Autowired
    private IManagerServiceGb managerService;

    @Value("${jwt.accessTime}")
    private Long accessTime;
    @Value("${jwt.refreshTime}")
    private Long refreshTime;
    @Value("${jwt.signature}")
    private String signature;

    @PostMapping("/login")
    public Result<TokenVo> login(@RequestBody ManagerDto managerDto, HttpServletRequest request) {
        //根据账号密码查询数据库
       Manager manager = managerService.login(managerDto.getAccountName(),managerDto.getPassword());
        Asserts.error(Objects.isNull(manager), BusinessCode.LOGIN_FAILED);

        //生成短token
        String accessToken = TokenUtil.createToken(manager.getId(), signature, accessTime, TimeUnit.MINUTES, request);

        //生成长token
        String refreshToken = TokenUtil.createToken(manager.getId(), signature, refreshTime, TimeUnit.DAYS, request);


        //返回对象
        TokenVo tokenVo = new TokenVo(manager.getId(), manager.getNickName(), accessToken,refreshToken);
        return Result.ok(tokenVo);
    }

    @RequestMapping("/findByPage")
    public Result<PageInfo<EmployeeDto>> findByPage(@RequestBody Map<String,Object> condition) {
        Integer pageNum = (Integer) condition.get("pageNum");
        Integer pageSize = (Integer) condition.get("pageSize");
        PageInfo<EmployeeDto> pageInfo = managerService.findByPage(condition, pageNum, pageSize);
        return Result.ok(pageInfo);
    }

    @RequestMapping("/add")
    public Result addManager(@RequestBody Manager manager) {
        int row = managerService.addManager(manager);
        return Result.ok(row);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Manager manager) {
        int row = managerService.update(manager);
        return Result.ok(row);
    }

    @RequestMapping("/deleteById")
    public Result updateById(Long id) {
        int row = managerService.deleteById(id);
        return Result.ok(row);
    }

    @RequestMapping("/findByAccountId")
    public Result findByAccountId(Long accountId) {
        List<Manager> byAccountId = managerService.findByAccountId(accountId);
        return Result.ok(byAccountId);
    }

}
